package cn.wyl.org.service.impl;

import cn.wyl.basic.exception.BusinessException;
import cn.wyl.basic.util.BaiduAuditUtils;
import cn.wyl.basic.util.Md5Utils;
import cn.wyl.basic.util.StrUtils;
import cn.wyl.org.domain.Employee;
import cn.wyl.org.domain.Shop;
import cn.wyl.org.domain.ShopAuditLog;
import cn.wyl.org.dto.ShopDto;
import cn.wyl.org.mapper.EmployeeMapper;
import cn.wyl.org.mapper.ShopAuditLogMapper;
import cn.wyl.org.mapper.ShopMapper;
import cn.wyl.org.service.IShopService;
import cn.wyl.basic.service.impl.BaseServiceImpl;
import cn.wyl.user.domain.Logininfo;
import cn.wyl.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void settlement(Shop shop) {
        //1.先添加t_employee => 自增长id
        //2.将自增长id设置到shop对象
        //3.再添加t_shop => 自增长id
        //4.将shop的自增长id设置到employee对象
        //5.修改t_employee

        //一：校验：空值校验，前端校验容易被禁用掉，需要后端校验
        if(StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("店铺名称不能为空!!!");
        }
        if(StringUtils.isEmpty(shop.getAddress())){
            throw new BusinessException("店铺地址不能为空!!!");
        }
        //2.校验：百度AI智能审核
        //审核文本
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称不合法!!!");
        }
        //审核图像：如果上传了图片 ，但是上传的图片不合法
        if(!StringUtils.isEmpty(shop.getLogo())
                && !BaiduAuditUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
            throw new BusinessException("店铺LOGO不合法!!!");
        }

        //3.校验当前店铺是否入驻--由店铺名称和地址校验
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if(dbShop !=null){
            throw new BusinessException("你的店铺已经入驻，不需要重复入驻!!");
        }

        //二：添加到数据库
        Employee employee = shop.getAdmin();
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //1.先添加t_logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//0-管理员
        logininfo.setDisable(false);//入驻之后才能使用
        logininfoMapper.save(logininfo);

        //2.再添加t_employee => 自增长id
        employee.setLogininfo_id(logininfo.getId());
        employee.setState(0);//先设置为禁用，审核通过并激活成功之后设置为1
        employeeMapper.save(employee);

        //2.将自增长id设置到shop对象
        shop.setAdminId(employee.getId());
        //3.再添加t_shop => 自增长id
        shopMapper.save(shop);

        //4.将shop的自增长id设置到employee对象
        employee.setShop_id(shop.getId());
        //5.修改t_employee
        employeeMapper.update(employee);
    }

    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        //1.改店铺状态：shop state 1 --> 2
        shopMapper.updateState(log.getShopId(),2);
        //2.添加审核日志
        log.setState(2);
        shopAuditLogMapper.save(log);
        //3.发送邮件：告诉店铺管理员激活邮件使用
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("862392110@qq.com");
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.findById(log.getShopId());
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208"+shop.getLogo()
                        +"' ><a href='http://127.0.0.1:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",
                true);
        Employee employee = employeeMapper.findById(shop.getAdminId());
        //收件人
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        //1.改店铺状态：shop state 1 --> 4
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(4);
        shopMapper.update(shop);
        //shopMapper.updateState(log.getShopId(),4);
        //2.添加审核日志
        log.setState(2);
        shopAuditLogMapper.save(log);
        //3.发送邮件：告诉店铺管理员入驻失败
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("862392110@qq.com");
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h1>你的店铺入驻失败，请重新入驻!!!</h1>", true);
        Employee employee = employeeMapper.findById(shop.getAdminId());
        //收件人
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }

    @Override
    public void active(Long id) {
        Shop shop = shopMapper.findById(id);
        shop.setState(3);
        shopMapper.update(shop);

        Employee employee = employeeMapper.findById(shop.getAdminId());
        employee.setState(1);
        employeeMapper.update(employee);

    }

}
